package civicinity.ist.psu.edu.share;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.utils.CivicinityDrawableManager;
import android.app.AlertDialog;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;

public class BalloonOverlayView extends FrameLayout{

	private ImageView speciesImage;
	private LinearLayout layout;
	private TextView description;
	private TextView commentCnt;
	private TextView likeCnt;
	private Context mContext;
	
	private ProgressBar mSpinner;

	/**
	 * Create a new BalloonOverlayView.
	 * 
	 * @param context - The activity context.
	 * @param balloonBottomOffset - The bottom padding (in pixels) to be applied
	 * when rendering this view.
	 */
	public BalloonOverlayView(Context context, int balloonBottomOffset) {

		super(context);
		mContext = context;

		setPadding(10, 0, 10, 30);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View v = inflater.inflate(R.layout.balloon_overlay, layout);
		
		description = (TextView) v.findViewById(R.id.balloon_item_description);
		commentCnt = (TextView) v.findViewById(R.id.balloon_item_comment_cnt);
		likeCnt = (TextView) v.findViewById(R.id.balloon_item_like_cnt);

		speciesImage = (ImageView) v.findViewById(R.id.thumb_nail);
		mSpinner = (ProgressBar) v.findViewById(R.id.spinner);
		
		
		/**
		 * ProgressBar spinner = (ProgressBar) v.findViewById(R.id.spinner);
			CivicinityDrawableManager dm = new CivicinityDrawableManager(mContext, spinner, speciesImage);
			dm.fetchDrawableOnThread(imagePath);
		 */
		
		// when click the close button
		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				layout.setVisibility(GONE);
			}
		});

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);
	}
	/**
	 * Sets the view data from a given overlay item.
	 * 
	 * @param item - The overlay item containing the relevant view data 
	 * (title and snippet). 
	 */
	
	public void setData(final CivicinityOverlayItem item) {

		layout.setVisibility(VISIBLE);
		
		// show the description
		if (item.getTitle() != null) {
			description.setVisibility(VISIBLE);
			description.setText(item.getTitle());
		} else {
			description.setVisibility(GONE);
		}
		
		// show the photo
		if (item.getSnippet() != null) {
			
			CivicinityDrawableManager dm = new CivicinityDrawableManager(mContext, mSpinner, speciesImage);
			dm.fetchDrawableOnThread(Constants.DOWNLOAD_SHARE_THUMBNAIL + item.getSnippet());
		} else {
			//address.setVisibility(GONE);
		}
		
		// show comment count
		commentCnt.setVisibility(View.VISIBLE);
		commentCnt.setText(item.getCommentCnt() + "");
		
		// show like count
		likeCnt.setVisibility(View.VISIBLE);
		likeCnt.setText(item.getIsLike() + "");
		
	}
}

